﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace CSharpToolWPF.Classes {
    class DropableRichTextBox : RichTextBox {
        public override void OnApplyTemplate() {
            base.OnApplyTemplate();

            Style = (Style)FindResource(typeof(RichTextBox));

            DragEnter += RichTextBox_DragEnter;
            Drop += RichTextBox_Drop;
            PreviewDragOver += RichTextBox_PreviewDragOver;
        }

        private void RichTextBox_PreviewDragOver(object sender, DragEventArgs e) {
            e.Handled = true;
        }

        private void RichTextBox_DragEnter(object sender, DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                e.Effects = DragDropEffects.Copy;
            }
            else
                e.Effects = DragDropEffects.None;
            e.Handled = true;
        }

        private void RichTextBox_Drop(object sender, DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                // 获取文件路径数组
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                Array.Sort(files);

                // 清空 RichTextBox 内容
                this.Document.Blocks.Clear();

                // 遍历文件路径并显示在 RichTextBox 中
                foreach (var file in files) {
                    Paragraph paragraph = new Paragraph(new Run(file));
                    this.Document.Blocks.Add(paragraph);
                }
            }
        }
    }
}
